package com.store.framework.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.store.framework.dao.IAppImagesDao;
import com.store.framework.entry.AppImagesTbl;

public class AppImagesDao implements IAppImagesDao {
	private static Log LOG = LogFactory.getLog(AppImagesDao.class);

	@Override
	public int add(AppImagesTbl d) {
		// TODO Auto-generated method stub
		int a =0;
		String sql ="insert into app_images_tbl(id,PerId,ImgScr,"
				+ "ImgSize,AddDate,AddPer,EditDate"
				+ ",EditPer) values(?,?,?,?,?,?,?,?)";
		try {
			DaoUtil.executeUpdate(sql, d.getId(),d.getPerId(),
					d.getImgScr(),d.getImgSize(),d.getAddDate(),
					d.getAddPer(),d.getEditDate(),d.getEditPer());
			a=1;
		} catch (SQLException e) {
			a=0;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		return a;
	}

	@Override
	public List<AppImagesTbl> select()  {
		List<AppImagesTbl> list =new ArrayList<AppImagesTbl>();
		String sql="select *from app_images_tbl";
		try {
			CachedRowSet cs=DaoUtil.SimpleQuery(sql);
			while(cs.next()){
				AppImagesTbl appimagestbl =new AppImagesTbl();
				appimagestbl.setPerId(cs.getString("ID"));
				appimagestbl.setPerId(cs.getString("PerId"));
				appimagestbl.setImgScr(cs.getString("ImgScr"));
				appimagestbl.setImgSize(cs.getString("ImgSize"));
				appimagestbl.setAddDate(cs.getDate("AddDate"));
				appimagestbl.setAddPer(cs.getString("AddPer"));
				appimagestbl.setEditDate(cs.getDate("EditDate"));
				appimagestbl.setEditPer(cs.getString("EditPer"));
				
				list.add(appimagestbl);
			}
		} catch (SQLException e) {
			list=null;
			LOG.error(e.getMessage(),e);
			throw new RuntimeException(e.getMessage(),e);
		}
		
		return list;
	}

}
